package com.hncc.dms.service.impl;

import java.util.List;

import com.hncc.dms.domain.DmsHerbalModelItem;
import com.hncc.dms.domain.DmsMedicineModelItem;
import com.hncc.dms.mapper.DmsHerbalModelItemMapper;
import com.hncc.dms.mapper.DmsMedicineModelItemMapper;
import com.hncc.sms.mapper.SysUserManagerMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.hncc.dms.mapper.DmsDrugModelMapper;
import com.hncc.dms.domain.DmsDrugModel;
import com.hncc.dms.service.IDmsDrugModelService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 药品模版管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-25
 */
@Service
public class DmsDrugModelServiceImpl implements IDmsDrugModelService 
{
    @Resource
    private DmsDrugModelMapper dmsDrugModelMapper;

    @Resource
    private SysUserManagerMapper sysUserManagerMapper;

    @Resource
    private DmsMedicineModelItemMapper dmsMedicineModelItemMapper;

    @Resource
    private DmsHerbalModelItemMapper dmsHerbalModelItemMapper;

    /**
     * 成药模板项对象
     */
    DmsMedicineModelItem dmsMedicineModelItem = new DmsMedicineModelItem();
    /**
     * 草药模板项对象
     */
    DmsHerbalModelItem dmsHerbalModelItem = new DmsHerbalModelItem();

    /**
     * 查询药品模版管理
     * 
     * @param id 药品模版管理ID
     * @return 药品模版管理
     */
    @Override
    public DmsDrugModel selectDmsDrugModelById(Long id)
    {
        return dmsDrugModelMapper.selectDmsDrugModelById(id);
    }

    /**
     * 查询药品模版管理列表
     * 
     * @param dmsDrugModel 药品模版管理
     * @return 药品模版管理
     */
    @Override
    public List<DmsDrugModel> selectDmsDrugModelList(DmsDrugModel dmsDrugModel)
    {
        return dmsDrugModelMapper.selectDmsDrugModelList(dmsDrugModel);
    }

    /**
     * 新增药品模版管理
     * @param dmsDrugModel 药品模版管理
     * @return
     */

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int insertDmsDrugModel(DmsDrugModel dmsDrugModel) {
        //根据登录名查询登录id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        //所属人id
        dmsDrugModel.setOwnId(userId);
        //创建时间
        dmsDrugModel.setCreateTime(DateUtils.getNowDate());
        //模板编码
        dmsDrugModel.setCode(DateUtils.dateTimeNow());

        //添加成药模板
        dmsDrugModelMapper.insertDmsDrugModel(dmsDrugModel);
        int num = 0;
        if(dmsDrugModel.getType() == 1){
            //添加成药模板项
            for (int i = 0;i < dmsDrugModel.getDmsMedicineModelItems().size();i++){
                dmsMedicineModelItem = dmsDrugModel.getDmsMedicineModelItems().get(i);
                //拿到模板id
                dmsMedicineModelItem.setModelId(dmsDrugModel.getId());
                // 拿到药品id
                dmsMedicineModelItem.setDrugId(dmsDrugModel.getDmsDrugs().get(i).getId());
                num = dmsMedicineModelItemMapper.insertDmsMedicineModelItem(dmsMedicineModelItem);
            }
        }else{
            //添加草药模板项
            for (int i = 0;i < dmsDrugModel.getDmsHerbalModelItems().size();i++){
                dmsHerbalModelItem = dmsDrugModel.getDmsHerbalModelItems().get(i);
                //拿到模板id
                dmsHerbalModelItem.setModelId(dmsDrugModel.getId());
                // 拿到药品id
                dmsHerbalModelItem.setDrugId(dmsDrugModel.getDmsDrugs().get(i).getId());
                num = dmsHerbalModelItemMapper.insertDmsHerbalModelItem(dmsHerbalModelItem);
            }
        }
        return num;
    }

    /**
     * 修改药品模版管理
     * 
     * @param dmsDrugModel 药品模版管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updateDmsDrugModel(DmsDrugModel dmsDrugModel)
    {
        if(dmsDrugModel.getType() == 1){
            //先删除 该模板的成药模板项
            dmsMedicineModelItemMapper.deleteDmsMedicineModelItemByModelId(dmsDrugModel.getId());
            //在添加 成药模板项
            for (int i = 0;i < dmsDrugModel.getDmsMedicineModelItems().size();i++){
                dmsMedicineModelItem = dmsDrugModel.getDmsMedicineModelItems().get(i);
                //拿到模板id
                dmsMedicineModelItem.setModelId(dmsDrugModel.getId());
                // 拿到药品id
                dmsMedicineModelItem.setDrugId(dmsDrugModel.getDmsDrugs().get(i).getId());
                dmsMedicineModelItemMapper.insertDmsMedicineModelItem(dmsMedicineModelItem);
            }
        }else{
            //先删除 该模板的草药模板项
            dmsHerbalModelItemMapper.deleteDmsHerbalModelItemByModelId(dmsDrugModel.getId());
            //添加草药模板项
            for (int i = 0;i < dmsDrugModel.getDmsHerbalModelItems().size();i++){
                dmsHerbalModelItem = dmsDrugModel.getDmsHerbalModelItems().get(i);
                //拿到模板id
                dmsHerbalModelItem.setModelId(dmsDrugModel.getId());
                // 拿到药品id
                dmsHerbalModelItem.setDrugId(dmsDrugModel.getDmsDrugs().get(i).getId());
                dmsHerbalModelItemMapper.insertDmsHerbalModelItem(dmsHerbalModelItem);
            }
        }
        return dmsDrugModelMapper.updateDmsDrugModel(dmsDrugModel);
    }

    /**
     * 批量删除药品模版管理
     * 
     * @param ids 需要删除的药品模版管理ID
     * @return 结果
     */
    @Override
    public int deleteDmsDrugModelByIds(Long[] ids)
    {
        return dmsDrugModelMapper.deleteDmsDrugModelByIds(ids);
    }

    /**
     * 删除药品模版管理信息
     * 
     * @param id 药品模版管理ID
     * @param type 模板类型
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int deleteDmsDrugModelById(Long id,Long type)
    {
        if(type == 1){
            //根据模板id删除相关的成药模板项
            dmsMedicineModelItemMapper.deleteDmsMedicineModelItemByModelId(id);
        }else{
            //根据模板id删除相关的草药模板项
            dmsHerbalModelItemMapper.deleteDmsHerbalModelItemByModelId(id);
        }
        return dmsDrugModelMapper.deleteDmsDrugModelById(id);
    }

    /**
     * 根据模板id查询三表数据(DmdDrug,DmsDrugModel,DmsMedicineModeItem)
     * @param id
     * @return
     */
    @Override
    public DmsDrugModel selectDmiById(Long id) {
        return dmsDrugModelMapper.selectDmiById(id);
    }

}
